package com.shushan.db;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.j256.ormlite.dao.Dao;
import com.shushan.lockscreen.bean.GameTimeBean;
import com.shushan.util.DateUtils;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: GameTimeDao
 * @Desciption: 定义GameTimeBean数据访问对象，对指定的表进行增删改查操作
 * @author: yichaohua
 * @date: 2018-06-14
 */
public class GameTimeDao {

    private static final String TAG = "GameTimeDao";

    private Dao<GameTimeBean, Integer> gameTimeDao;
    private DBHelper dbHelper;

    /**
     * 构造方法
     * 获得数据库帮助类实例，通过传入Class对象得到相应的Dao
     * @param context
     */
    public GameTimeDao(Context context) {
        try {
            dbHelper = DBHelper.getHelper(context);
            gameTimeDao = dbHelper.getDao(GameTimeBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if(gameTimeDao == null){
            Log.e(TAG,"gameTimeDao == null");
        }
    }

    /**
     * 添加一条记录
     * @param gameTimeBean
     */
    public void add(GameTimeBean gameTimeBean) {
        try {
            gameTimeDao.create(gameTimeBean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除一条记录
     * @param gameTimeBean
     */
    public void delete(GameTimeBean gameTimeBean) {
        try {
            gameTimeDao.delete(gameTimeBean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 更新一条记录
     * @param gameTimeBean
     */
    public void update(GameTimeBean gameTimeBean) {
        try {
            gameTimeDao.update(gameTimeBean);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询一条记录
     * @param id
     * @return
     */
    public GameTimeBean queryForId(int id) {
        GameTimeBean gameTimeBean = null;
        try {
            gameTimeBean = gameTimeDao.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return gameTimeBean;
    }

    public List<GameTimeBean> queryForPaperId(int paperId) {
        List<GameTimeBean> gameTimeBeanList = new ArrayList<GameTimeBean>();
        try {
            gameTimeBeanList = gameTimeDao.queryBuilder().where().eq("paperId",paperId).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return gameTimeBeanList;
    }

    public List<GameTimeBean> queryForPaperIdNow(int paperId) {
        String nowDate = DateUtils.getNowDate("yyyyMMdd");
        List<GameTimeBean> gameTimeBeanList = new ArrayList<GameTimeBean>();
        try {
            gameTimeBeanList = gameTimeDao.queryBuilder().where().eq("paperId",paperId)
                    .and().eq("createDate",nowDate).query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return gameTimeBeanList;
    }

    /**
     * 查询所有记录
     * @return
     */
    public List<GameTimeBean> queryForAll() {
        List<GameTimeBean> gameTimeBeanList = new ArrayList<GameTimeBean>();
        try {
            gameTimeBeanList = gameTimeDao.queryForAll();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return gameTimeBeanList;
    }

    public boolean containPaperId(int paperId){
        if(paperId <= 0){
            return false;
        }

        List<GameTimeBean> gameTimeBeanList = queryForAll();
        for (GameTimeBean gameTimeBean : gameTimeBeanList){
            if(gameTimeBean.getPaperId() == paperId){
                return true;
            }
        }

        return false;
    }


    public void clearTab(){
        try {
            gameTimeDao.queryRaw("delete from game_time_table");
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    public GameTimeBean getGameTimeBean(int currentChildId,String usePage,List<GameTimeBean> gameTimeBeanList){
        if(currentChildId <= 0 || TextUtils.isEmpty(usePage) ||gameTimeBeanList == null || gameTimeBeanList.size() == 0){
            return null;
        }

        for (GameTimeBean gameTimeBean : gameTimeBeanList){
            if((gameTimeBean.getChildId() == currentChildId) && gameTimeBean.getUsePackName().equals(usePage)){
                return gameTimeBean;
            }
        }

        return null;
    }

    public boolean containChildId(int currentChildId,List<GameTimeBean> gameTimeBeanList){
        if(currentChildId <= 0 || gameTimeBeanList == null || gameTimeBeanList.size() == 0){
            return false;
        }

        for (GameTimeBean gameTimeBean : gameTimeBeanList){
            if(gameTimeBean.getChildId() == currentChildId){
                return true;
            }
        }

        return false;
    }

    public boolean containUsePage(String usePage,List<GameTimeBean> gameTimeBeanList){
        if(TextUtils.isEmpty(usePage) || gameTimeBeanList == null || gameTimeBeanList.size() == 0){
            return false;
        }

        for (GameTimeBean gameTimeBean : gameTimeBeanList){
            if(gameTimeBean.getUsePackName().equals(usePage)){
                return true;
            }
        }

        return false;
    }

}
